package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.service.api.CourseService;
import com.cwj.mansysback.service.api.ElectiveService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.CourseServiceImpl;
import com.cwj.mansysback.service.impl.ElectiveServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UpdateCourseServlet extends HttpServlet {

    private static final String ADD = "/Lock/Add/Course";
    private static final String UPDATE = "/Lock/Update/Course";
    private static final String DELETE = "/Lock/Delete/Course";

    private static final CourseService couSer = new CourseServiceImpl();
    private static final ElectiveService eleSer = new ElectiveServiceImpl();
    private static final TeacherService teaSer = new TeacherServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("------------------------- UpdateCourse start----------------------");
        Statement statement = new Statement(8);

        boolean isUpdate = req.getRequestURI().contains(UPDATE);
        boolean isInsert = req.getRequestURI().contains(ADD);
        boolean isDelete = req.getRequestURI().contains(DELETE);
        ;

        String cid = req.getParameter("cid");

        System.out.println(cid);

        if (cid == null) {
            MyUtils.writeResponse(resp, statement);
            return;
        }

        if (isUpdate || isInsert) {

            String cname = req.getParameter("cname");
            String credit = req.getParameter("credit");
            String tid = req.getParameter("tid");

            System.out.println(cname);
            System.out.println(credit);
            System.out.println(tid);

            if (cname != null && credit != null && tid != null) {
                if (isInsert) {
                    System.out.println(teaSer.getTeacher(tid));
                    if (couSer.selectCourse(cid) != null) {
                        System.out.println("重复");
                        statement.setStatue(13);
                    } else if (teaSer.getTeacher(tid) == null) {
                        System.out.println("教师编号不存在");
                        statement.setStatue(13);
                        statement.setMessage("教师编号不存在");
                    } else {
                        System.out.println("插入");
                        if (couSer.insertCourse(cid, cname, tid, Integer.valueOf(credit))) {
                            statement.setStatue(0);
                        }
                    }
                } else {
                    System.out.println("update");
                    if (couSer.selectCourse(cid) == null) statement.setStatue(14);
                    else if (couSer.updateCourse(cid, cname, tid, Integer.valueOf(credit))) {
                        statement.setStatue(0);
                    }
                }
            }


        } else if (isDelete) {
            if (eleSer.selectPackBySid(cid).getCount() > 0) statement.setStatue(10);
            else if (couSer.deleteCourse(cid)) {
                statement.setStatue(0);
            }
        }

        MyUtils.writeResponse(resp, statement);

        System.out.println("------------------------- UpdateCourse start----------------------");
    }
}
